﻿using System.Collections.Generic;
using System.Linq;
using Dexter.DataTransferObject;
using Dexter.Domain.Results;

namespace Dexter.Domain.Model {
	public static class PostHelper {
		public static IList<PostDto> ToDtos(this IEnumerable<Post> s)
		{
			return s.Select ( x => x.ToDto ( ) ).ToList (  );
		}

		public static PostDto ToDto ( this Post s ) {
			var r = new PostDto ( );

			r.Abstract = s.Abstract;
			r.BreakOnAggregate = s.BreakOnAggregate;
			r.CommentEnabled = s.CommentEnabled;
			r.FormattedBody = s.FormattedBody;
			r.Id = s.Id;
			r.PublishDate = s.PublishDate;
			r.ShowOnAggregate = s.ShowOnAggregate;
			r.ShowOnSearch = s.ShowOnSearch;
			r.Slug = s.Slug;
			r.Tags = s.Tags.Select ( x => x.Name ).ToArray ( );
			r.Title = s.Title;
			r.TotalComments = s.TotalComments;
			r.TotalTrackback = s.TotalTrackback;
			r.Username = s.Username;

			r.TrackBacks = s.TrackBacks.ToDtos();

			r.Comments = s.Comments.ToDtos ( );
			r.Categories = s.Categories.ToDtos ( );

			return r;
		}

		public static IEnumerable<PostLightDto> ToLightDtos ( this IEnumerable<Item> s ) {
			return s.Select ( x => x.ToDtoLight ( ) ).ToList (  );
		}

		public static PostLightDto ToDtoLight ( this Item s ) {
			var r = new PostLightDto ( );

			r.Id = s.Id;
			r.Slug = s.Slug;
			r.Title = s.Title;

			return r;
		}

		public static IPagedResult <PostDto> ToDtos(this IPagedResult <Post> s) {
			return new PagedResult <PostDto> ( s.PageIndex , s.PageSize , s.Result.ToDtos ( ) , s.TotalCount );
		}
	}
}